<template>
  <div>
    <el-card style="margin: 20px 0px">
      <CategorySelect @getCategoryId="getCategoryId"></CategorySelect>
    </el-card>
    <el-card>
      <div v-show="isShowTable">
        <el-button type="primary" icon="el-icon-plus" style="margin: 0px 0px 20px 0px" :disabled="!category3Id"
          @click="addAttr">添加属性</el-button>
        <el-table border style="width: 100%" :data="attrList">
          <el-table-column type="index" label="序号" width="80px" align="center">
          </el-table-column>
          <el-table-column prop="attrName" label="属性名称" width="150">
          </el-table-column>
          <el-table-column label="属性值列表">
            <template slot-scope="{ row, $index }">
              <el-tag style="margin: 5px 10px" type="success" v-for="(attrValue, index) in row.attrValueList"
                :key="attrValue.id">
                {{ attrValue.valueName }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="250">
            <template slot-scope="{ row, $index }">
              <el-button type="warning" icon="el-icon-edit" size="mini" @click="updataAttrInfo(row)">修改</el-button>
              <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 添加和修改属性的结构 -->
      <div v-show="!isShowTable">
        <el-form :inline="true" ref="form" label-width="80px" :model="attrInfo">
          <el-form-item label="属性名">
            <el-input placeholder="请输入属性名" v-model="attrInfo.attrName"> </el-input>
          </el-form-item>
        </el-form>
        <el-button type="primary" icon="el-icon-plus" @click="addAttrValue()"
          :disabled="!attrInfo.attrName">添加属性值</el-button>
        <el-button @click="isShowTable = true">取消</el-button>
        <el-table style="widows: 100%;margin: 20px 0px;" border :data="attrInfo.attrValueList">
          <el-table-column align="center" type="index" label="序号" width="80">
          </el-table-column>
          <el-table-column width="width" prop="prop" label="属性值名称">
            <template slot-scope="{row,$index}">
              <el-input v-model="row.valueName" placeholder="请输入属性值" size="mini" v-if="row.flag" @blur="toLook(row)"
                @keyup.native.enter="toLook(row)" :ref="$index"></el-input>
              <span @click="toEdit(row, $index)" v-else style="display: block;">{{ row.valueName }}</span>
            </template>
          </el-table-column>
          <el-table-column width="width" prop="prop" label="操作">
            <template slot-scope="{row,$index}">
              <el-popconfirm :title="`确定删除${row.valueName}`" @onConfirm="deleteAttrValue($index)">
                <el-button type="danger" size="mini" icon="el-icon-delete" slot="reference"></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-button type="primary" @click="addOrUpdateAttr">保存</el-button>
        <el-button @click="isShowTable = true"> 取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import cloneDeep from 'lodash/cloneDeep'
export default {

  data() {
    return {
      isShowTable: true,
      category1Id: "",
      category2Id: "",
      category3Id: "",
      // 属性列表
      attrList: [],
      // 
      attrInfo: {
        attrName: "",
        attrValueList: [

        ],
        categoryId: 0,  // 三级分类的id
        categoryLevel: 3,

      }
      /*
             {
            attrId: 0,
            valueName: ""
          }
      */
    };
  },
  name: "Attr",

  methods: {
    getCategoryId({
      categoryId,
      level
    }) {
      if (level == 1) {
        this.category1Id = categoryId;
        this.category2Id = "";
        this.category3Id = "";
      } else if (level == 2) {
        this.category2Id = categoryId;
        this.category3Id = "";
      } else {
        // 代表三级分类有了可以发请求了
        this.category3Id = categoryId;
        this.getAttrList();
      }
    },
    // 获取平台属性数据
    async getAttrList() {
      const {
        category1Id,
        category2Id,
        category3Id
      } = this;
      let reslut = await this.$API.attr.reqAttrList(
        category1Id,
        category2Id,
        category3Id
      );
      if (reslut.code == 200) {
        this.attrList = reslut.data;

      }
    },
    addAttrValue() {
      this.attrInfo.attrValueList.push({
        attrId: this.attrInfo.id,
        valueName: "",
        flag: true, // 切换查看与编辑模式

      })

      this.$nextTick(() => {
        this.$refs[this.attrInfo.attrValueList.length - 1].focus();
      });


    },
    addAttr() {
      this.attrInfo = {
        attrName: "",
        attrValueList: [

        ],
        categoryId: this.category3Id,  // 三级分类的id
        categoryLevel: 3,

      }
      this.isShowTable = false
    },
    updataAttrInfo(row) {
      this.isShowTable = false
      this.attrInfo = cloneDeep(row)
      this.attrInfo.attrValueList.forEach((item) => {
        this.$set(item, 'flag', false);
      })
    },
    // 查看模式
    toLook(row) {
      // row 是当前用户添加的最新的属性值
      if (row.valueName.trim() == "") {
        this.$message('属性值不能为空')
        return;
      }
      let isRepat = this.attrInfo.attrValueList.some((item) => {
        if (row !== item) {
          return row.valueName == item.valueName
        }
      })
      console.log(isRepat);
      if (isRepat) {
        this.$message('属性值不能重复')
        return;
      }

      row.flag = false
    },

    toEdit(row, index) {
      row.flag = true
      this.$nextTick(() => {
        this.$refs[index].focus();
      });

    },
    // 气泡确认框
    deleteAttrValue(index) {
      this.attrInfo.attrValueList.splice(index, 1)
    },


    async addOrUpdateAttr() {
      // 整理参数
      this.attrInfo.attrValueList = this.attrInfo.attrValueList.filter(item => {
        if (item.valueName != '') {
          delete item.flag
          return true
        }
      })
      try {
        await this.$API.attr.reqAddOrUpdateAttr(this.attrInfo);
        this.isShowTable = true;

        this.$message({ type: 'success', message: '保存成功' });
        this.getAttrList();
      } catch (error) {

      }
    },

  },
};
</script>

<style scoped>

</style>
